package com.whfc.common.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.ArrayList;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/28 17:54
 */
public class MathUtil {

    /**
     * 保留小数位
     *
     * @param digit 数字
     * @param scale 小数位
     * @return
     */
    public static double round(double digit, int scale) {
        if (Double.isNaN(digit) || Double.isInfinite(digit)) {
            return 0D;
        }
        return new BigDecimal(digit).setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }

    /**
     * 保留小数位
     *
     * @param digit 数字
     * @param scale 小数位
     * @return
     */
    public static float round(float digit, int scale) {
        if (Double.isNaN(digit) || Double.isInfinite(digit)) {
            return 0F;
        }
        return new BigDecimal(digit).setScale(scale, RoundingMode.HALF_UP).floatValue();
    }

    /**
     * 数据类型转换
     *
     * @param f
     * @return
     */
    public static Double float2Double(Float f) {
        if (f == null) {
            return null;
        }
        BigDecimal b = new BigDecimal(String.valueOf(f));
        return b.doubleValue();
    }

    /**
     * 数据类型转换
     *
     * @param f
     * @return
     */
    public static Float double2Float(Double f) {
        if (f == null) {
            return null;
        }
        BigDecimal b = new BigDecimal(String.valueOf(f));
        return b.floatValue();
    }

    /**
     * 数据类型转换
     *
     * @param f
     * @return
     */
    public static int doule2int(Double f) {
        if (f == null) {
            return 0;
        }
        BigDecimal b = new BigDecimal(String.valueOf(f));
        return b.intValue();
    }

    /**
     * 数据类型转换
     *
     * @param l
     * @return
     */
    public static int long2int(Long l) {
        if (l == null) {
            return 0;
        }
        BigInteger b = new BigInteger(String.valueOf(l));
        return b.intValue();
    }

    /**
     * 数据类型转换
     *
     * @param f
     * @return
     */
    public static double int2double(Integer f) {
        if (f == null) {
            return 0;
        }
        BigDecimal b = new BigDecimal(String.valueOf(f));
        return b.doubleValue();
    }

    /**
     * 除法
     *
     * @param value1
     * @param value2
     * @param scale
     * @return
     */
    public static double divide(Integer value1, Integer value2, Integer scale) {
        if (value2 == null || value2 == 0) {
            return 0;
        }
        scale = scale != null ? scale : 0;
        value1 = value1 != null ? value1 : 0;
        BigDecimal bigA = BigDecimal.valueOf(Double.valueOf(value1));
        BigDecimal bigB = BigDecimal.valueOf(Double.valueOf(value2));
        return bigA.divide(bigB, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 除法
     *
     * @param value1
     * @param value2
     * @param scale
     * @return
     */
    public static int divideInt(Integer value1, Integer value2, Integer scale) {
        if (value2 == null || value2 == 0) {
            return 0;
        }
        scale = scale != null ? scale : 0;
        value1 = value1 != null ? value1 : 0;
        BigDecimal bigA = BigDecimal.valueOf(Double.valueOf(value1));
        BigDecimal bigB = BigDecimal.valueOf(Double.valueOf(value2));
        return bigA.divide(bigB, scale, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public static void main(String[] args) {
        Float a = 1f;
        int b = new ArrayList<>().size();
        System.out.println(round(a / b, 2));

        System.out.println(divide(0, 0, 2));
        System.out.println(divide(null, 0, 2));
        System.out.println(divide(0, null, 2));

        System.out.println(divideInt(0, 0, 2));
        System.out.println(divideInt(null, 1, 2));
        System.out.println(divideInt(1, null, 2));

        System.out.println(long2int(100l));
    }
}
